﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace Speedy
{
    public class dbDevices : MasterTable
    {
        public Column id { get; set; }
        public Column name { get; set; }
        public Column device_code { get; set; }
        public Column license_code { get; set; }

        public dbDevices()
        {
            this.id = new Column(typeof(int), "ID", 4, false, true);
            this.name = new Column(typeof(string), "DESCRIPTION", 50);
            this.device_code = new Column(typeof(string), "DEVICE_CODE", 50);
            this.license_code = new Column(typeof(string), "LICENCE_CODE", 50);

            this.TableName = "DEVICE_DEFINITIONS";
            this.Columns.Add(this.id);
            this.Columns.Add(this.name);
            this.Columns.Add(this.device_code);
            this.Columns.Add(this.license_code);
        }
    }


    public class dbDeviceParameters : MasterTable
    {
        public Column device_id { get; set; }
        public Column paramCode { get; set; }
        public Column paramValue { get; set; }

        public dbDeviceParameters()
        {
            this.device_id = new Column(typeof(int), "DEVICE_ID", 4, false, true);
            this.paramCode = new Column(typeof(string), "PARAMETER_CODE", 25, false, true);
            this.paramValue = new Column(typeof(string), "PARAMETER_VALUE", 25, false);

            this.TableName = "DEVICE_PARAMETERS";
            this.Columns.Add(this.device_id);
            this.Columns.Add(this.paramCode);
            this.Columns.Add(this.paramValue);
        }

        public override DataTable AllData()
        {
            string where = string.Empty;
            string select = string.Empty;

            ParameterCollection prm = new ParameterCollection();
            foreach (Column col in this.Columns)
            {
                if (col.Name == "DEVICE_ID")
                {
                    where += col.Name + "=@" + col.Name + " and ";
                    prm.AddItem(Prm.Get("@" + col.Name, col.Read()));
                }
                select += col.Name + ",";
            }

            where = where.Remove(where.Length - 4, 4);
            select = select.Remove(select.Length - 1, 1);

            StringBuilder sb = new StringBuilder();
            sb.Append(string.Format("select {0} from {1} where {2} ", select, this.TableName, where));

            return DataLayer.GetLayer.FillData(sb.ToString(), prm.ToArray());
        }

        public override void Remove()
        {
            string where = string.Empty;
            ParameterCollection prm = new ParameterCollection();

            foreach (Column col in this.Columns)
            {
                if (col.Name == "DEVICE_ID")
                {
                    where += col.Name + "=@" + col.Name + " and ";
                    prm.AddItem(Prm.Get("@" + col.Name, col.Read()));
                    break;
                }
            }

            where = where.Remove(where.Length - 4, 4);

            StringBuilder sb = new StringBuilder();
            sb.Append(string.Format("delete from {0} where {1} ", this.TableName, where));

            DataLayer.GetLayer.ExecQuery(sb.ToString(), prm.ToArray());

        }

    }

}
